﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System.ComponentModel;
using System.Diagnostics;

namespace Xceed.Silverlight.Data
{
  [DebuggerDisplay( "FilterBy = {DebuggerDisplayString}" )]
  public abstract class FilterExpressionBase 
  {
    #region Constructor

    internal FilterExpressionBase()
    {
    }

    #endregion

    #region IsFrozen Property

    internal bool IsFrozen
    {
      get
      {
        return m_isFrozen;
      }
    }

    private bool m_isFrozen; //false

    #endregion

    #region DebuggerDisplayString Internal Property

    internal string DebuggerDisplayString
    {
      get
      {
        FilterExpressionFormatter formatter = new DebuggerDisplayFilterExpressionFormatter( this );

        return formatter.ToString();
      }
    }

    #endregion

    [DebuggerStepThrough]
    public static bool operator ==(
      FilterExpressionBase left,
      FilterExpressionBase right )
    {
      return object.Equals( left, right );
    }

    [DebuggerStepThrough]
    public static bool operator !=(
      FilterExpressionBase left,
      FilterExpressionBase right )
    {
      return !object.Equals( left, right );
    }

    public override int GetHashCode()
    {
#if WINDOWS_PHONE
      this.Freeze();
#endif

      return base.GetHashCode();
    }

    public override bool Equals( object obj )
    {
#if WINDOWS_PHONE
      this.Freeze();
#endif

      return base.Equals( obj );
    }

    internal abstract FilterExpressionBase Clone();

    internal abstract void Accept( FilterExpressionVisitor visitor );

    internal void Freeze()
    {
      if( m_isFrozen )
        return;

      //Any errors while freezing will throw out of this method up to the action that caused the freeze.
      this.FreezeOverride();

      m_isFrozen = true;
    }

    internal virtual void FreezeOverride()
    {
    }
  }
}
